import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:iidx_progress_tool/components/musiclistcomponent/filter_model.dart';

import '../../data/consts/version.dart';

class VersionDropdownMenu extends StatefulWidget {
  FilterModel filterModel;

  VersionDropdownMenu({required this.filterModel});

  @override
  State<StatefulWidget> createState() {
    return _VersionDropdownMenuState();
  }
}

class _VersionDropdownMenuState extends State<VersionDropdownMenu> {
  var _dropdownMenuController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return DropdownMenu<Version> (
      dropdownMenuEntries: Version.values.map<DropdownMenuEntry<Version>>((Version v) {
        return DropdownMenuEntry(value: v, label: v.name);
      }).toList(),
      onSelected: (value) {
        setState(() {
          widget.filterModel.onVersionSelected(value);
        });
      },
      leadingIcon: IconButton(
        icon: Icon(Icons.clear),
        onPressed: _clearSelection,
      ),
      controller: _dropdownMenuController,
      menuHeight: 500,
      width: MediaQuery.of(context).size.width * 0.5,
      hintText: '选择版本',
    );
  }

  void _clearSelection() {
    _dropdownMenuController.clear();
    widget.filterModel.onVersionSelected(null);
  }
}

